package com.hudongwx.food.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.activity.ShouYeTiaoZhuanActivity;
import com.hudongwx.food.item.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:降初
 * DateTime:16-12-13 下午6:41
 */
public class MygridviewAdapter extends BaseAdapter {
    Context context;
    JSONArray gridView;
    public MygridviewAdapter(JSONArray gridView,Context context) {
        this.gridView = gridView;
        this.context=context;
    }


    @Override
    public int getCount() {
        return gridView.length();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (null == convertView) {
            myViewHolder = new MyViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.home_classification_1,null);
            myViewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_classification);
            myViewHolder.tv = (TextView) convertView.findViewById(R.id.tv_classification);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        try {
            x.image().bind(myViewHolder.iv, gridView.getJSONObject(position).getString("IconUrl"));
            myViewHolder.tv.setText(gridView.getJSONObject(position).getString("Alias"));
            myViewHolder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ShouYeTiaoZhuanActivity.class);
                    intent.putExtra("type",position);
                    context.startActivity(intent);

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    class MyViewHolder {
        ImageView iv;
        TextView tv;
    }
}
